package heima.lm.oschina.vpfragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.R;
import heima.lm.oschina.adapter.BlogRvAdapter;
import heima.lm.oschina.base.BaseFragment;
import heima.lm.oschina.bean.Blog;
import heima.lm.oschina.bean.BlogList;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.interf.OnRecyclerViewItemClickListener;
import heima.lm.oschina.utils.HttpUtils;
import heima.lm.oschina.utils.SpUtil;
import heima.lm.oschina.utils.UIUtils;

/**
 * Created by herenxue on 2017/4/1.
 * 推荐
 */

public class RecommendFragment extends BaseFragment {

    @BindView(R.id.rv_general_successful)
    RecyclerView mRecyclerView;
    @BindView(R.id.srl_general_successful)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private List<Blog> mList;
    private BlogRvAdapter mAdapter;


    @Override
    protected void loadSuccessPager() {
        View successfulView = View.inflate(UIUtils.getContext(), R.layout.fragment_general_successful, null);
        //绑定successfulView
        ButterKnife.bind(this, successfulView);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(UIUtils.getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new BlogRvAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        initEvent();
        mCommonPager.changeView(successfulView);
    }

    public void initEvent() {
        mAdapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Blog blog = (Blog) mAdapter.getDatas().get(position);
                String id = String.valueOf(blog.getId());
                String ids = SpUtil.getString(Constants.NEWS_IDS, "");
                if (!ids.contains(id)) {
                    ids += ids + "," + id;
                    SpUtil.saveString(Constants.NEWS_IDS, ids);
                }
                TextView tvBlogTitle = (TextView) v.findViewById(R.id.tv_blog_title);
                tvBlogTitle.setTextColor(UIUtils.getColor(R.color.gray));
                TextView tvBlogContent = (TextView) v.findViewById(R.id.tv_blog_body);
                tvBlogContent.setTextColor(UIUtils.getColor(R.color.gray));
                Intent intent = new Intent("heima.lm.os.china.detail.blog");
                intent.putExtra("blog_detail_id", String.valueOf(blog.getId()));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        mCommonPager.isReadData = true;
        HttpUtils<BlogList> httpUtils = new HttpUtils<>();
        Map<String, String> parmas = new HashMap<>();
        parmas.put("page", "0");
        httpUtils.getData(Constants.Http.RECOMMEND, parmas, BlogList.class, new HttpUtils.OnHttpResponseListener<BlogList>() {
            @Override
            public void onSuccess(BlogList bean) {
                mList = bean.getList();
                mCommonPager.dealCommonSuccess(mList);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.isReadData = false;
                mCommonPager.dealCommonFailure();
            }
        });
    }

}
